﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle(QStringLiteral("服务器"));
    ui->lineEdit_Port->setText("8848"); //默认端口
    ui->pushButton_Send->setEnabled(false);
    server = new QTcpServer();
    //连接
    connect(server,&QTcpServer::newConnection,this,&MainWindow::server_New_Connection);
}

MainWindow::~MainWindow()
{
    server->close();
    server->deleteLater();
    delete ui;
}
/*************************************************
     Description:    侦听客户端连接或取消侦听
     return:         void
     Auther:         任占宇
     Time:           2021-1-30
   *************************************************/
void MainWindow::on_pushButton_Listen_clicked()
{
    if(ui->pushButton_Listen->text() == QString (QStringLiteral("侦听")))
    {
        //从输入框获取端口号
        int port = ui->lineEdit_Port->text().toInt();
        //监听指定的端口
        if(!server->listen(QHostAddress::Any,port))
        {
            qDebug()<<server->errorString();
        }
        ui->pushButton_Listen->setText(QStringLiteral("取消侦听"));
        qDebug()<<"Listen Success!";

    }
    else
    {
        //如果正确连接
        if(socket->state() == QAbstractSocket::ConnectedState)
        {
            //关闭连接
            socket->disconnectFromHost();
         }
        //取消监听
        server->close();
        ui->pushButton_Listen->setText(QStringLiteral("侦听"));
        //发送按键失能
        ui->pushButton_Send->setEnabled(false);
    }
}
/*************************************************
     Description:    向客户端发送消息
     return:         void
     Auther:         任占宇
     Time:           2021-1-30
   *************************************************/
void MainWindow::on_pushButton_Send_clicked()
{
    qDebug()<<"Send: "<<ui->textEdit_Send->toPlainText();
    socket->write(ui->textEdit_Send->toPlainText().toLocal8Bit());
    socket->flush();
}
/*************************************************
     Description:    连接客户端
     return:         void
     Auther:         任占宇
     Time:           2021-1-30
   *************************************************/
void MainWindow::server_New_Connection()
{
    //获取客户端的连接
    socket = server->nextPendingConnection();
    //连接QTcpScoket信号槽，读取新数据
    QObject::connect(socket,&QTcpSocket::readyRead,this,&MainWindow::socket_Read_Data);
    QObject::connect(socket,&QTcpSocket::disconnected,this,&MainWindow::socket_Disconnection);
    //发送按键使能
    ui->pushButton_Send->setEnabled(true);
    qDebug()<<"A Client connect!";
}
/*************************************************
     Description:    接收客户端消息
     return:         void
     Auther:         任占宇
     Time:           2021-1-30
   *************************************************/
void MainWindow::socket_Read_Data()
{
    QByteArray buffer;
    //读取数据缓冲中的数据
    buffer = socket->readAll();
    if(!buffer.isEmpty()){
        QString str = ui->textEdit_Recv->toPlainText();
        QString strRecv = QString::fromLocal8Bit(buffer);
        str+=strRecv+"\n";
        //刷新显示
        ui->textEdit_Recv->setText(str);
    }
}
/*************************************************
     Description:    取消侦听后
     return:         void
     Auther:         任占宇
     Time:           2021-1-30
   *************************************************/
void MainWindow::socket_Disconnection()
{
    //发送按键失能
    ui->pushButton_Send->setEnabled(false);
    qDebug()<<"Disconnection";
}
